package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.MyForum;
import com.ruoyi.system.domain.MyForumImg;
import com.ruoyi.system.domain.MyUser;
import com.ruoyi.system.domain.dto.ForumDTO;
import com.ruoyi.system.mapper.MyForumImgMapper;
import com.ruoyi.system.mapper.MyForumMapper;
import com.ruoyi.system.mapper.MyUserMapper;
import com.ruoyi.system.service.IMyForumService;
import com.ruoyi.system.service.IMyUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MyForumServiceImpl extends ServiceImpl<MyForumMapper, MyForum> implements IMyForumService  {

    @Autowired
    private MyForumMapper myForumMapper;
    @Autowired
    private MyForumImgMapper myForumImgMapper;
    @Autowired
    private MyUserMapper myUserMapper;

    /**
     * 查询论坛列表
     */
    @Override
    public List<MyForum> selectMyForumList(MyForum myForum) {
        LambdaQueryWrapper<MyForum> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(myForum.getType() != null && myForum.getType() != -1, MyForum::getType, myForum.getType())
                .eq(myForum.getUid() != null, MyForum::getUid, myForum.getUid())
                .orderByDesc(MyForum::getFtime);
        List<MyForum> list = myForumMapper.selectList(queryWrapper);
        List<MyForum> collect = list.stream().map(item -> {
            MyUser myUser = myUserMapper.selectById(item.getUid());
            item.setNickname(myUser.getNickname());
            item.setHeadimg(myUser.getHeadimg());

            LambdaQueryWrapper<MyForumImg> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(MyForumImg::getFid, item.getId());
            List<MyForumImg> myForumImgs = myForumImgMapper.selectList(queryWrapper1);
            String imgs = myForumImgs.stream().map(MyForumImg::getImg).collect(Collectors.joining(","));
            item.setImgs(imgs);
            return item;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取论坛详细信息
     */
    @Override
    public MyForum selectMyForumById(Long id) {
        return null;
    }

    /**
     * 删除论坛
     */
    @Override
    public int deleteMyForumById(Long id) {
        LambdaQueryWrapper<MyForumImg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MyForumImg::getFid, id);
        int deleted = myForumImgMapper.delete(queryWrapper);
        deleted += myForumMapper.deleteById(id);
        return deleted;
    }

    @Override
    @Transactional
    public int issueForum(ForumDTO forumDTO) {
        MyForum myForum = new MyForum();
        BeanUtils.copyProperties(forumDTO, myForum);
        myForum.setFtime(LocalDateTime.now());
        int inserted = myForumMapper.insert(myForum);

        for (Map<String, String> img : forumDTO.getImgs()) {
            MyForumImg myForumImg = new MyForumImg();
            myForumImg.setFid(myForum.getId());
            myForumImg.setImg(img.get("url"));
            inserted += myForumImgMapper.insert(myForumImg);
        }
        return inserted;
    }
}